package com.tjjingpan.jetpack

import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/*感知Activity生命周期
*
* */
class MyObserver(val lifecycle:Lifecycle):LifecycleObserver {
    //在方法上使用了OnLifecycleEvent注解，并传入了一种生命周期事件。
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun activityStart(){
        Log.d("MyObserver","activityStart")
        Log.d("MyObserver",lifecycle.currentState.toString())
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun activityStop() {
        Log.d("MyObserver","activityStop")
        Log.d("MyObserver",lifecycle.currentState.toString())
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun activityResume() {
        Log.d("MyObserver","activityResume")
        Log.d("MyObserver",lifecycle.currentState.toString())
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun activityDestroy() {
        Log.d("MyObserver","activityDestroy")
        Log.d("MyObserver",lifecycle.currentState.toString())
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun activityPause() {
        Log.d("MyObserver","activityPause")
        Log.d("MyObserver",lifecycle.currentState.toString())
    }




    //借助LifecycleOwner这个好帮手，使用如下的语法结构让MyObserver得到通知
    // lifecycleOwner.lifecycle.addObserver(MyObserver())
}